/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesam.gedeon.service.impl;

import com.tesam.gedeon.dao.CountryDao;
import com.tesam.gedeon.entity.Country;
import com.tesam.gedeon.service.CountryService;
import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.commons.lang3.StringEscapeUtils;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Sistema
 */
@Service
public class CountryServiceImpl implements CountryService {

    @Autowired
    public CountryDao countryDao ; 
    
    @Override
    public List<Country> getListAll() { 
        return countryDao.getListAll() ;
    }
    
    @Override
    public int getCountAll(){
    return countryDao.getCountAll() ;
    }

    @Override
    public void updateCountry(Country country){
         countryDao.updateCountry(country);
    }
    
    @Override
    public List<Country> getAllByLimit(int startLimit, int endLimit, String dataToSort, String directionOfOrder) {
      return countryDao.getAllByLimit(startLimit, endLimit, dataToSort, directionOfOrder);
    }
    
    public List<Country> getSearchCountryByLimit(int startLimit, int endLimit, String dataToSort, String directionOfOrder, Country country) {
        return countryDao.getSearchCountryByLimit(startLimit, endLimit, dataToSort, directionOfOrder, country);
    }

    @Override
    public List<Country> getCountryById(Country country) {
        return countryDao.getCountryById(country);
    }

    @Override
    public int saveCountry(Country country) {
        return countryDao.saveCountry(country);
    }

    @Override
    public List<Country> deleteCountry(Country country) {
        return countryDao.deleteCountry(country);
    }

    @Override
    public int getTotalBySearch(Country country) {
      return countryDao.getTotalBySearch(country);
    }
    
    @Override
    public String getJsonDataFromArrayList(List<Country> listCountry, int countRecord) {

        JSONArray array = null;
        JSONObject json = null;
        String deleteButton = "" ;
        String updateButton = "" ;
        String viewButton = "" ;
        SimpleDateFormat format = null;

        try {

            json = new JSONObject();
            array = new JSONArray();

            if (listCountry.size() > 0) {


                format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // Date date = sdf.parse("2004-07-24 09:45:52.189");
              
                for (Country country : listCountry) {
                    JSONArray arrayPut = new JSONArray();
 
                    viewButton = "<a href='view?id="+country.getId()+"&module=20' ";
                    viewButton+= "class='btn mini blue'><i class='icon-share'></i> View </a>&nbsp;";
                    
                    updateButton = "<a href='update?id="+country.getId()+"&module=20' class='btn mini green' >" ;
                    updateButton+= "<i class='icon-pencil'></i> Update</a>&nbsp;";
                    
                    deleteButton = "<a class='btn mini red' data-toggle='modal' ";
                    deleteButton+= "onclick=\"";     
                    deleteButton+="confirmDelete('"+country.getId()+"')";
                    deleteButton+="\" "; 
                    deleteButton+="href='#static' >";
                    deleteButton+="<i class='icon-trash'></i> Delete</a>";      
                        
                    arrayPut.put(country.getCode());
                    arrayPut.put(country.getLongCode());
                    arrayPut.put(StringEscapeUtils.escapeHtml4(country.getName()));
                    arrayPut.put(viewButton+updateButton+deleteButton);
                    array.put(arrayPut);
                }
            }

            json.put("iTotalRecords", countRecord);
            json.put("iTotalDisplayRecords", countRecord);
            json.put("aaData", array);

            return json.toString();
        } catch (Exception e) {
            System.out.println("Exception getJsonDataFromArrayList:" + e.getMessage());
            return null;
        }
    }

    
}
